using System;
using UnityEngine;

[RequireComponent(typeof(MeshRenderer))]
[RequireComponent(typeof(MeshFilter))]
[ExecuteInEditMode]
public class PlaneMeshObject : MonoBehaviour
{
	public Color color
	{
		get
		{
			if (this.colors == null)
			{
				return Color.white;
			}
			return this.colors[0];
		}
		set
		{
			if (this.colors == null)
			{
				return;
			}
			if (this.color == value)
			{
				return;
			}
			for (int i = 0; i < this.colors.Length; i++)
			{
				this.colors[i] = value;
			}
			this.BuildMesh();
		}
	}

	private void Start()
	{
		this.BuildMesh();
	}

	private void Update()
	{
	}

	public void BuildMesh()
	{
		MeshFilter component = base.gameObject.GetComponent<MeshFilter>();
		Mesh mesh = new Mesh();
		mesh.vertices = new Vector3[]
		{
			Vector3.right * this.region.xMin * this.scale.x + Vector3.up * this.region.yMin * this.scale.y,
			Vector3.right * this.region.xMin * this.scale.x + Vector3.up * this.region.yMax * this.scale.y,
			Vector3.right * this.region.xMax * this.scale.x + Vector3.up * this.region.yMin * this.scale.y,
			Vector3.right * this.region.xMax * this.scale.x + Vector3.up * this.region.yMax * this.scale.y
		};
		mesh.uv = new Vector2[]
		{
			new Vector2(this.uv.xMin, this.uv.yMin),
			new Vector2(this.uv.xMin, this.uv.yMax),
			new Vector2(this.uv.xMax, this.uv.yMin),
			new Vector2(this.uv.xMax, this.uv.yMax)
		};
		mesh.triangles = new int[]
		{
			0,
			1,
			3,
			0,
			3,
			2
		};
		if (this.colors != null && this.colors.Length == mesh.vertices.Length)
		{
			mesh.colors = this.colors;
		}
		mesh.RecalculateBounds();
		mesh.RecalculateNormals();
		component.mesh = mesh;
		Mesh sharedMesh = component.sharedMesh;
		sharedMesh.RecalculateBounds();
		sharedMesh.RecalculateNormals();
	}

	public Rect region = new Rect(-1f, -1f, 2f, 2f);

	public Rect uv = new Rect(0f, 0f, 1f, 1f);

	public Vector3 scale = Vector3.one;

	public Color[] colors;
}
